package com.project.modules.notice.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.project.common.api.CommonPage;
import com.project.common.api.CommonResult;
import com.project.modules.notice.model.TblMessage;
import com.project.modules.notice.service.TblMessageService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * Controller
 *
 * @author Qing2514
 */
@RestController
@Api(value = "TblMessageController", tags = "管理")
@RequestMapping("/message")
public class TblMessageController {

    @Autowired
    private TblMessageService messageService;

    @ApiOperation("根据ID查询")
    @GetMapping("/{id}")
    public CommonResult<TblMessage> getById(@PathVariable Long id) {
        TblMessage message = messageService.getById(id);
        return CommonResult.success(message);
    }

    @ApiOperation("根据关键字分页模糊查询")
    @GetMapping(value = "/page")
    public CommonResult<CommonPage<TblMessage>> getPage(@RequestParam(value = "keyword", required = false) Long keyword,
                                                        @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                                                        @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum) {
        Page<TblMessage> list = messageService.getPage(keyword, pageSize, pageNum);
        return CommonResult.success(CommonPage.restPage(list));
    }

    @ApiOperation("添加")
    @PostMapping("")
    public CommonResult<Object> create(@RequestBody TblMessage message) {
        boolean success = messageService.save(message);
        return success ? CommonResult.success() : CommonResult.failed();
    }

    @ApiOperation("修改")
    @PutMapping("")
    public CommonResult<Object> update(@RequestBody TblMessage message) {
        boolean success = messageService.updateById(message);
        return success ? CommonResult.success() : CommonResult.failed();
    }

    @ApiOperation("根据ID删除")
    @DeleteMapping("/{id}")
    public CommonResult<Object> delete(@PathVariable Long id) {
        boolean success = messageService.removeById(id);
        return success ? CommonResult.success() : CommonResult.failed();
    }

}
